package com.rfsp.common.init;

import java.io.IOException;
import java.io.InputStream;
import java.util.TreeMap;
import java.util.UUID;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

/**
 * @author zhangchao
 *
 */
@Component("v")
@Order(value = 9)
public class Version implements ApplicationRunner {

	Logger logger = LoggerFactory.getLogger(Version.class);

	@Autowired
	WebApplicationContext wac;

	/**
	 * 工程版本号
	 */
	@Value("${application.version}")
	public String version;

	@Override
	public void run(ApplicationArguments args) throws Exception {

		logger.info("\n\n\n");
		ServletContext sc = wac.getServletContext();
		try(InputStream in = sc.getResourceAsStream("/META-INF/MANIFEST.MF")){
			if(in!=null) {
				Manifest mf = new Manifest(in);
				Attributes as = mf.getMainAttributes();
				TreeMap<String, String> ts = new TreeMap<>();
				as.forEach((k,v)->ts.put(k.toString(), v.toString()));
				ts.forEach((k,v) -> logger.info("----------------------------{}={}", k, v));
			}
		}catch(IOException e){
			logger.error(e.getMessage());
		}
		logger.info("\n\n\n----------------------------rfsp-mcp.version={}----------------------------\n\n\n", version);
	}
}
